package com.idp.web.shop.service.impl;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.idp.common.persistence.Page;
import com.idp.common.util.ResourceUtils;
import com.idp.web.shop.dao.CategoryDao;
import com.idp.web.shop.entity.Category;
import com.idp.web.shop.service.CategoryService;

/**
 * 
 * 商品分类service实现类
 * 	2018-04-12 WuJiangWu
 */
@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {

	@Resource
	private CategoryDao categoryDao;
	
	@Override
	public List<Category> find(Category category) {
		return categoryDao.find(category);
	}
	
	@Override
	public Page<Category> findByPage(Category category, Page<Category> page) {

		page.setResult(categoryDao.find(category, page));
		
		return page;
	}
	
	@Override
	public List<Category> findBySearch(Category category) {

		return categoryDao.find(category);
	}
	
	@Override
	public Category getById(String id) {

		return categoryDao.getById(id);
	}

	@Override
	public void add(Category category) {
		categoryDao.add(category);
	}
	
	@Override
	public void update(Category category) {

		categoryDao.update(category);
	}

	@Override
	public void delete(String id) {
		deleteChildren(id);
		categoryDao.delete(id);
	}
	
	public void deleteChildren(String id){
		List<Category> children = categoryDao.getByParentId(id);
		if(children != null && children.size() > 0){
			for(Category child : children){
				deleteChildren(child.getId());
			}
			categoryDao.delete(id);
		}
	}
	
	@Override
	public List<Category> getByParentId(String parentId){
		
		return categoryDao.getByParentId(parentId);
	}
	
	@Override
	public List<Category> findForTreeTable(String parentId) {
		
		return categoryDao.findForTreeTable(parentId);
	}
 	
}